package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategy")
public class    StrategyController {
    @Autowired
    private IStrategyService service;
    @Autowired
    private IRegionService regionService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQueryObject qo) {
        PageInfo<Strategy> pageInfo = service.selectList(qo);
        model.addAttribute("result", pageInfo);
        model.addAttribute("regions", regionService.selectAll());

        return "strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Strategy strategy, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = QiniuCloudUtil.uploadQiniu(img);
            strategy.setCoverUrl(url);
        }
        service.saveOrUpdate(strategy);
        return "redirect:/strategy/list.do";

    }

}
